import { handle } from "./canvas";

/**
 * @description 模拟后端接口。
 * 文档：https://github.com/cgfeel/react-rotate-captcha?tab=readme-ov-file
 */
const tokenRaw = "Nvuv8LdXUNRAVW022Gm7HkGc7RTDoEmU";
const info = {
  angle: -1,
  sid: "",
  ticket: "",
};


export async function get(imgUrl) {
  info.angle = -1;
  info.sid = "";
  info.ticket = "";
  return {
    code: 0,
    data: {
      str: imgUrl,
      token: tokenRaw,
    },
    msg: "success",
  };
}



export async function load(path) {
  const [degree, src] = await handle(path);
  info.angle = degree;

  return src;
}



export async function verify(token, deg) {
  const { angle } = info;
  const success = token === tokenRaw && Math.abs(deg - angle) <= 10;

  info.sid = Math.random().toString(36).slice(-8);
  // info.ticket = crypto.randomUUID();
  info.ticket = 'test';

  return angle >= 0 && success
    ? {
        code: 0,
        data: {
          sid: info.sid,
          ticket: info.ticket,
        },
        msg: "Success",
      }
    : {
        code: 1,
        msg: "Fail verify",
      };
}
